//***********************************************************
//******************AJAX**************************************
//***********************************************************
var ajax = function(){

	var objectContext = this;
	var xhr = null;
	var params = url = method = request = null;

	var _default = {

		success: objectContext.success,
		url: null,
		method: "GET",
		request: null,
		async: false

	}
	//***********************************************************
	this.initialize = function(initParams){

		params = extend(_default, initParams);

		url 	= params.url;
		method  = params.method;
		request = params.request;

		//Set Callbacks
		if(initParams.success != null && initParams.success != undefined){
			objectContext.success = initParams.success;
		}
		if(initParams.error != null && initParams.error != undefined){
			objectContext.error = initParams.error;
		}

		//Create XHR object
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xhr= new XMLHttpRequest();
		}else {// code for IE6, IE5
		 	xhr= new ActiveXObject("Microsoft.XMLHTTP");
		}

		xhr.onreadystatechange = function(xhrEvent){
			
			//var readyState = ((xhr.readyState > 0) ? 4: 0;

			if(xhr.readyState == 4){  //XHR finished loading response
				var response = null;

				if( xhr.status==200 || xhr.status==300 || xhr.status==304){ //Success
					if(xhrEvent.currentTarget.response != null && xhrEvent.currentTarget.response != undefined){
						try{
							eval("response=" + xhrEvent.currentTarget.responseText);
						}catch(e){
							response = xhrEvent.currentTarget.response;
						}
					}

					objectContext.success(response, xhrEvent.currentTarget);		
				}else{ //ERROR
					objectContext.error(xhrEvent.currentTarget.response, xhrEvent.currentTarget);
				}
			}
		}
	}
	//***********************************************************
	this.call = function(_url, _request, _method){

		url 	= (_url != undefined) ? _url : params.url;
		request = (_request != undefined) ? _request : params.request;
		method = (_method != undefined) ? _method : params.method;

		
		
		if(method == "GET" || ( request != null && request != undefined )){		
			url = parseParamatrizedUrl(url, request);
		}

		xhr.open(method, url, params.async);

		xhr.send(request);
		
	}

	//***********************************************************
	this.set = function(property, value){
		if(objectContext[property] != undefined && value != undefined){
			objectContext[property] = value;
		}else{
			console.log("Unkown property " + property + " or undefined value.");
		}
	}
	//***********************************************************
	this.get = function(property){
		if(objectContext[property] != undefined){
			return objectContext[property];
		}else{
			console.log("Unkown property " + property + " or undefined value.");
		}
	}

	//***********************************************************
	this.success = function(response, xhrObject){
		console.dir(response);
	}
	//***********************************************************
	this.error = function(response, xhrObject){
		throw new Error("Ajax call failed:" + xhrObject.status);
		console.dir(xhrObject);
	}

	return this;

}

//***********************************************************
//******************AJAX**************************************
//***********************************************************
var jsonp = function(){

	this.initialize = function(){

	}

}




